// const BASE_URL = "http://192.168.10.122:8081"; //张
const BASE_URL = "http://192.168.10.243:8081";//刘


export default {
	post(url, params) {
		wx.showLoading({
			title: '请求中',
			mask: true
		})
		let promise = new Promise((resolve, rejct) => {
			//1、拿到当前登录信息中的token
			let loginUser = wx.getStorageSync('userInfo');
			let token = "";
			if (loginUser) {
				token = loginUser.token
			}
			wx.request({
				url: `${BASE_URL}${url}`,
				method: "POST",
				data: params,
				header: {
					'content-type': 'application/json', // 默认值
					"satoken": token
				},
				success: (res) => {
					if (res.data.code == 500) {
						wx.showToast({
							title: res.data.msg,
							icon: 'error',
							duration: 2000
						})
					} else if (res.data.code == 200) {
						resolve(res.data);
					} else if (res.data.code == 401) {
						wx.showToast({
							title: '用户登录失效',
							icon: 'error',
							duration: 2000
						})
						setTimeout(() => {
							uni.navigateTo({
								url: '/pages/public/login'
							})
						}, 2000)

					}
				},
				fail(err) {
					rejct(err);
				},
				complete() {
					wx.hideLoading();
				}
			})
		});
		return promise;
	},
	get(url, params) {
		wx.showLoading({
			title: '请求中',
			mask: true
		})
		let promise = new Promise((resolve, rejct) => {
			//1、拿到当前登录信息中的token
			let loginUser = wx.getStorageSync('userInfo');
			let token = "";
			if (loginUser) {
				token = loginUser.token
			}
			wx.request({
				url: `${BASE_URL}${url}`,
				method: "GET",
				data: params,
				header: {
					'content-type': 'application/json', // 默认值
					"satoken": token
				},
				success: (res) => {
					if (res.data.code == 500) {
						wx.showToast({
							title: res.data.msg,
							icon: 'error',
							duration: 2000
						})
					} else if (res.data.code == 200) {
						resolve(res.data);
					} else if (res.data.code == 401) {
						wx.showToast({
							title: '用户登录失效',
							icon: 'error',
							duration: 2000
						})
						setTimeout(() => {
							uni.navigateTo({
								url: '/pages/public/login'
							})
						}, 2000)
					}
				},
				fail(err) {
					rejct(err);
				},
				complete() {
					wx.hideLoading();
				}
			})
		});
		return promise;
	}
}